package hhm.android.library.utils

import android.app.DatePickerDialog
import android.content.Context
import hhm.android.library.datePicker.DatePickDialog
import hhm.android.library.datePicker.bean.DateParams
import hhm.android.library.weightHeightPicker.heightPicker.HeightPickDialog
import hhm.android.library.weightHeightPicker.weightPicker.WeightPickDialog
import java.text.SimpleDateFormat
import java.util.*

class DialogUtils {

    companion object {
        /**
         * @description 选择日期弹出框
         * @param listener 选择日期确定后执行的接口
         * @param curDate 当前显示的日期

        showDatePickDialog(
        this,
        { _, year, month, dayOfMonth ->
        activity_library_two_select_birthday.text = "$year-${month + 1}-$dayOfMonth"
        }, activity_library_two_select_birthday.text.toString()
        )

         */
        fun showDatePicDialog(
            context: Context,
            listener: DatePickerDialog.OnDateSetListener?,
            curDate: String
        ) {
            val calendar: Calendar = Calendar.getInstance()
            var year: Int
            var month: Int
            var day: Int
            try {
                year = curDate.substring(0, curDate.indexOf("-")).toInt()
                month =
                    curDate.substring(curDate.indexOf("-") + 1, curDate.lastIndexOf("-"))
                        .toInt() - 1
                day = curDate.substring(curDate.lastIndexOf("-") + 1, curDate.length).toInt()
            } catch (e: Exception) {
                e.printStackTrace()
                year = calendar.get(Calendar.YEAR)
                month = calendar.get(Calendar.MONTH)
                day = calendar.get(Calendar.DAY_OF_MONTH)
            }
            val datePicker =
                DatePickerDialog(
                    context,
                    DatePickerDialog.THEME_HOLO_LIGHT,
                    listener,
                    year,
                    month,
                    day
                )
            datePicker.show()
        }


        /**
        DialogUtils.showDatePicBottomView(
        this,
        object : GetDateListener {
        override fun getDate(date: Date, string: String) {
        }
        },
        DateParams.TYPE_YEAR, DateParams.TYPE_MONTH, DateParams.TYPE_DAY,
        )
         */
        fun showDatePicBottomView(
            context: Context,
            listener: GetDateListener?,
            sdf: String = "yyyy/MM/dd",
            @DateParams.Type vararg style: Int
        ) {
            val todayCal = Calendar.getInstance()
            val startCal = Calendar.getInstance()
            val endCal = Calendar.getInstance()
            startCal.add(Calendar.YEAR, -99)
            endCal.add(Calendar.YEAR, 1)
            DatePickDialog.Builder()
                .setTypes(style)
                .setCurrentDate(todayCal.time)
                .setStartDate(startCal.time)
                .setEndDate(endCal.time)
                .setOnSureListener { date ->
                    val message = SimpleDateFormat(sdf).format(date)
                    listener?.getDate(date, message)
                }
                .show(context)
        }

        fun showDatePicBottomView(
            context: Context,
            dateNow: Long,
            listener: GetDateListener?,
            sdf: String = "yyyy/MM/dd",
            @DateParams.Type vararg style: Int
        ) {
            val todayTime = Date(dateNow)
            val startCal = Calendar.getInstance()
            val endCal = Calendar.getInstance()
            startCal.add(Calendar.YEAR, -99)
            endCal.add(Calendar.YEAR, 1)
            DatePickDialog.Builder()
                .setTypes(style)
                .setCurrentDate(todayTime)
                .setStartDate(startCal.time)
                .setEndDate(endCal.time)
                .setOnSureListener { date ->
                    val message = SimpleDateFormat(sdf).format(date)
                    listener?.getDate(date, message)
                }
                .show(context)
        }

        /**
        DialogUtils.showHeightPicBottomView(this, object : GetStringListener {
        override fun getString(s: String) {
        Toast.makeText(this@MainActivity, s, Toast.LENGTH_LONG).show()
        }
        })
         */
        fun showHeightPicBottomView(
            context: Context,
            type: String,
            listener: GetStringListener?
        ) {
            HeightPickDialog.Builder(type).setOnSureListener(object : GetStringListener {
                override fun getString(s: String) {
                    listener?.getString(s)
                }
            }).show(context)
        }

        fun showHeightPicBottomView(
            context: Context,
            type: String,
            data: Double,
            listener: GetStringListener?
        ) {
            HeightPickDialog.Builder(type).setInitData(data)
                .setOnSureListener(object : GetStringListener {
                    override fun getString(s: String) {
                        listener?.getString(s)
                    }
                }).show(context)
        }

        /**
        DialogUtils.showWeightPicBottomView(this, object : GetStringListener {
        override fun getString(s: String) {
        Toast.makeText(this@MainActivity, s, Toast.LENGTH_LONG).show()
        }
        })
         */
        fun showWeightPicBottomView(
            context: Context,
            type: String,
            listener: GetStringListener?
        ) {
            WeightPickDialog.Builder(type)
                .setOnSureListener(object : GetStringListener {
                    override fun getString(s: String) {
                        listener?.getString(s)
                    }
                }).show(context)
        }

        fun showWeightPicBottomView(
            context: Context,
            type: String,
            data: Double,
            listener: GetStringListener?
        ) {
            WeightPickDialog.Builder(type).setInitData(data)
                .setOnSureListener(object : GetStringListener {
                    override fun getString(s: String) {
                        listener?.getString(s)
                    }
                }).show(context)
        }
    }
}